package com.example.caoben_back.contoller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.caoben_back.entity.po.Collect;
import com.example.caoben_back.entity.po.User;
import com.example.caoben_back.entity.vo.Result;
import com.example.caoben_back.service.ICollectService;
import com.example.caoben_back.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private ICollectService collectService;
    
    // 收藏帖子
    @PostMapping("/add")
    public Result<Boolean> collectPost(@RequestParam Integer postId,
                                      HttpServletRequest request) {
        // 从token中解析用户信息
        User user = TokenUtil.getUserFromRequest(request);
        if (user == null) {
            return Result.fail("用户未登录");
        }
        Integer userId = user.getUserId();
        
        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setPostId(postId);
        collect.setCreateTime(LocalDateTime.now());
        boolean save = collectService.save(collect);
        return Result.success(save);
    }

    // 取消收藏
    @PostMapping("/cancel")
    public Result<Boolean> cancelCollect(@RequestParam Integer postId,
                                        HttpServletRequest request) {
        // 从token中解析用户信息
        User user = TokenUtil.getUserFromRequest(request);
        if (user == null) {
            return Result.fail("用户未登录");
        }
        Integer userId = user.getUserId();
        
        boolean remove = collectService.remove(new QueryWrapper<Collect>()
                .eq("user_id", userId)
                .eq("post_id", postId));
        return Result.success(remove);
    }

    // 获取用户收藏列表
    @GetMapping("/list")
    public Result<Map<String, Object>> getUserCollects(@RequestParam Integer userId,
                                                     @RequestParam Integer page,
                                                     @RequestParam Integer size) {
        Page<Collect> collectPage = collectService.page(new Page<>(page, size),
                new QueryWrapper<Collect>().eq("user_id", userId).orderByDesc("create_time"));
        Map<String, Object> data = new HashMap<>();
        data.put("list", collectPage.getRecords());
        data.put("total", collectPage.getTotal());
        return Result.success(data);
    }

    // 检查是否已收藏
    @GetMapping("/check")
    public Result<Map<String, Boolean>> checkCollect(@RequestParam Integer postId,
                                                    HttpServletRequest request) {
        // 从token中解析用户信息
        User user = TokenUtil.getUserFromRequest(request);
        if (user == null) {
            return Result.fail("用户未登录");
        }
        Integer userId = user.getUserId();
        
        Collect collect = collectService.getOne(new QueryWrapper<Collect>()
                .eq("user_id", userId)
                .eq("post_id", postId));
        Map<String, Boolean> data = new HashMap<>();
        data.put("isCollect", collect != null);
        return Result.success(data);
    }
}